package ar.edu.unq.wannaplay.pages.abm.projects;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.dtos.EmployeeDTO;
import ar.edu.unq.wannaplay.dtos.ProjectDTO;
import ar.edu.unq.wannaplay.dtos.SkillDTO;
import ar.edu.unq.wannaplay.employee.Experience;
import ar.edu.unq.wannaplay.pages.ProjectPage;
import ar.edu.unq.wannaplay.pages.abm.WPABM;
import ar.edu.unq.wannaplay.services.ProjectService;
import ar.edu.unq.wannaplay.tools.EmployeeSelector;
import ar.edu.unq.wannaplay.tools.SkillSelector;
import ar.edu.unq.wannaplay.tools.SortManager;

public class ModifyProjectPage extends WPABM<ProjectDTO, ProjectPage> {
    private static final long serialVersionUID = 1L;

    private List<SkillDTO> newSkills;

    private EmployeeSelector myEmployees, otherEmployees;

    @SpringBean(name = "service.projectService")
    private ProjectService projectService;

    public ModifyProjectPage(final Panel response, final PageParameters parameter) {
    	super(ProjectPage.class);
        ProjectDTO abmObject = getProjectService().getProjectDTOById(parameter.getString("project"));
        this.setAbmObject(abmObject);
        otherEmployees = new EmployeeSelector().withAllEmployeesThatDoesntWorkIn(abmObject);
        myEmployees = new EmployeeSelector().withAllEmployeesFrom(abmObject);
        newSkills = abmObject.getRequirements();

        this.init();
    }

    protected void init() {
        WebMarkupContainer container = new WebMarkupContainer("data");

        this.addData(container);

        this.addEmployeeLists(container);

        this.addSkillsTable(container);

        this.addButtons();
    }

    private void addEmployeeLists(final WebMarkupContainer container) {
        WebMarkupContainer employeesContainer = new WebMarkupContainer("employeeDiv");
        container.add(employeesContainer);

        employeesContainer.add(new PageableListView<EmployeeDTO>("myEmployeesTable", this.getMyEmployees()
                .getEmployees(), 5) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<EmployeeDTO> item) {
                EmployeeDTO employee = item.getModelObject();
                item.add(new Label("name", employee.getName()));
                item.add(new Label("surname", employee.getSurname()));
                item.add(new Label("id", employee.getDni()));
            }
        });

        employeesContainer.add(new Link<WebPage>("addEmployee") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                ModifyProjectPage.this.getMyEmployees().addEmployee(
                        ModifyProjectPage.this.getOtherEmployees().getSelectedEmployee());
            }
        });
        employeesContainer.add(new Link<WebPage>("removeEmployee") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                ModifyProjectPage.this.getMyEmployees().removeEmployee(
                        ModifyProjectPage.this.getMyEmployees().getSelectedEmployee());
            }
        });

        employeesContainer.add(new PageableListView<EmployeeDTO>("otherEmployeesTable", this.getOtherEmployees()
                .getEmployees(), 5) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<EmployeeDTO> item) {
                EmployeeDTO employee = item.getModelObject();
                item.add(new Label("name", employee.getName()));
                item.add(new Label("surname", employee.getSurname()));
                item.add(new Label("id", employee.getDni()));
            }
        });

        employeesContainer.add(new Link<WebPage>("filterByRecommended") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                ModifyProjectPage.this.getOtherEmployees().setEmployees(
                        ModifyProjectPage.this.getProjectService().filterEmployeesByRecommended(
                                ModifyProjectPage.this.getAbmObject(),
                                ModifyProjectPage.this.getOtherEmployees().getEmployees()));
                ModifyProjectPage.this.init();
            }
        });

        employeesContainer.add(new Link<WebPage>("filterByName") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                ModifyProjectPage.this.getOtherEmployees().setEmployees(
                        SortManager.getInstance()
                                .sortByName(ModifyProjectPage.this.getOtherEmployees().getEmployees()));
                ModifyProjectPage.this.init();
            }
        });
    }

    private void addData(final WebMarkupContainer container) {
        container.add(new TextField<ProjectDTO>("name", new PropertyModel<ProjectDTO>(this.getAbmObject(), "name")));
        container.add(new TextField<ProjectDTO>("manHour",
                new PropertyModel<ProjectDTO>(this.getAbmObject(), "manHour")));
        TextField<ProjectDTO> projectStart = new TextField<ProjectDTO>("from", new PropertyModel<ProjectDTO>(
                this.getAbmObject(), "projectStart"));
        TextField<ProjectDTO> projectEnd = new TextField<ProjectDTO>("to", new PropertyModel<ProjectDTO>(
                this.getAbmObject(), "projectEnd"));
        projectStart.add(new DatePicker());
        projectEnd.add(new DatePicker());
        container.add(projectStart);
        container.add(projectEnd);
    }

    protected void addSkillsTable(final WebMarkupContainer container) {
        container.add(new PageableListView<SkillDTO>("requirementsTable", this.getNewSkills(), 5) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<SkillDTO> item) {
                SkillDTO skill = item.getModelObject();
                item.add(new Label("skillName", skill.getKnowledge()));
                item.add(new Label("skillExp", skill.getExperience().toString()));
            }
        });

        final SkillSelector skillSelector = new SkillSelector();
        container.add(new TextField<String>("newSkillName", new PropertyModel<String>(skillSelector, "name")));
        container.add(new DropDownChoice<Experience>("newSkillExp", new PropertyModel<Experience>(skillSelector,
                "experience"), skillSelector.getExperiences()));

        container.add(new Link<WebPage>("addSkill") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                ModifyProjectPage.this.getNewSkills().add(
                        new SkillDTO(skillSelector.getExperience(), skillSelector.getName()));
                ModifyProjectPage.this.addSkillsTable(container);
            }
        });
    }

    @Override
    protected void accept() {
        this.getProjectService().deleteById(this.getAbmObject().getId());

        this.getAbmObject().setRequirements(this.getNewSkills());
        this.getAbmObject().setEmployees(this.getMyEmployees().getEmployees());
        this.getProjectService().save(this.getAbmObject());
    }

    // setters&getters
    public void setProjectService(final ProjectService projectService) {
        this.projectService = projectService;
    }

    public ProjectService getProjectService() {
        return projectService;
    }

    public void setNewSkills(final List<SkillDTO> newSkills) {
        this.newSkills = newSkills;
    }

    public List<SkillDTO> getNewSkills() {
        return newSkills;
    }

    public void setMyEmployees(final EmployeeSelector myEmployees) {
        this.myEmployees = myEmployees;
    }

    public EmployeeSelector getMyEmployees() {
        return myEmployees;
    }

    public void setOtherEmployees(final EmployeeSelector otherEmployees) {
        this.otherEmployees = otherEmployees;
    }

    public EmployeeSelector getOtherEmployees() {
        return otherEmployees;
    }
}
